//链表分割
//现有一链表的头指针 ListNode* pHead，给一定值x，编写一段代码将所有小于x的结点排在其余结点之前
//，且不能改变原来的数据顺序，返回重新排列后的链表的头指针。
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        if (!pHead || !pHead->next)
            return NULL;
        ListNode lessHead(0);
        ListNode greateHead(0);
        ListNode* tail_less = &lessHead;
        ListNode* tail_greate = &greateHead;

        while (pHead) {
            if (pHead->val < x) {
                tail_less->next = pHead;
                tail_less = tail_less->next;
            }
            else {
                tail_greate->next = pHead;
                tail_greate = tail_greate->next;
            }
            pHead = pHead->next;
        }
        tail_less->next = greateHead.next;
        tail_greate->next = NULL;
        return lessHead.next;
    }
};